<?php



class users{
	static $_instance = null;

	public static function getInstance() {
		if (self::$_instance == null) {
			self::$_instance = new users();
		}
		return self::$_instance;
	}

	function getSessionToken($facebookuid){
		//esto habria que incluirlo de dbconfig.php pero no me anda
		$dbhost = 'mysql.gosocial.com.ar';
		$dbuser = 'socialitba';
		$dbpass = 's0c14l1tb4';
		$dbname = 'socialitba';

		$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die('Can not connect to database! Please try again later');
		mysql_select_db($dbname, $conn);

		//escapar la variable???? estaria copado
		$result = mysql_query("SELECT token, active FROM bundundu_users WHERE fb_id=" . $facebookuid . ";");

		$row = mysql_fetch_array($result);

		mysql_close($conn);

		if($row==null | $row['active']==0)
			return null;
				
		
		return $row['token'];
	}
	
	/*function getSessionToken($facebookuid){
		
		if( ($token = $this->getGoogleCalendarToken($facebookuid)) !=null){
		
			//devuelve el session token directamente:
			return	Zend_Gdata_AuthSub::getAuthSubSessionToken($token);	
		}
		return null;
	}*/
	

	function registerGoogleCalendarAccount($facebookuid, $token){
		
		
		
		//esto habria que incluirlo de dbconfig.php pero no me anda
		$dbhost = 'mysql.gosocial.com.ar';
		$dbuser = 'socialitba';
		$dbpass = 's0c14l1tb4';
		$dbname = 'socialitba';
		
		$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die('Can not connect to database! Please try again later');
		mysql_select_db($dbname, $conn);

		$result = mysql_query("SELECT * FROM bundundu_users WHERE fb_id=" . $facebookuid . ";");
		$row = mysql_fetch_array($result);
		if($row==null)
		{
			//escapar las variables???? aca seria obligatorio...
			$result = mysql_query("INSERT INTO bundundu_users(fb_id, token, active) VALUES (" . $facebookuid . ",'" . $token . "'," ."  1);");
		}
		else
		{
			//escapar las variables???? aca seria obligatorio...
			$result = mysql_query("UPDATE bundundu_users SET token='" . $token . "', active=1 WHERE fb_id=" . $facebookuid . ";");
		}

		mysql_close($conn);
	}
	
	function logout($facebookuid)
	{
		$dbhost = 'mysql.gosocial.com.ar';
		$dbuser = 'socialitba';
		$dbpass = 's0c14l1tb4';
		$dbname = 'socialitba';
		
		$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die('Can not connect to database! Please try again later');
		mysql_select_db($dbname, $conn);

		$result = mysql_query("UPDATE bundundu_users SET active=0 WHERE fb_id=" . $facebookuid . ";");

		mysql_close($conn);
	}
}
